﻿using System;
using System.Collections.Generic; 
using System.Text;
using ATSModel;
using ATSIDAL;
using System.Data.SqlClient;
using System.Data;

namespace ATSDAL
{
    public class AirtightnessDal : BaseRepository_DT<AirtightnessInfo>, IAirtightness
    {
        #region 静态变量部分
        private const string ARM_PRODUCTTYPE_PRODUCTSN = "@ProductSN";
        private const string ARM_PRODUCTTYPE_RECORDTIME = "@RecordTime";
        private const string ARM_PRODUCTTYPE_ID = "@ProductTypeID";
        private const string ARM_PRODUCTTYPE_USERID = "@UserID";
        private const string ARM_PRODUCTTYPE_PCIP = "@PCIP";
        private const string ARM_PRODUCTTYPE_TESTVALUE = "@TestValue";
        private const string ARM_PRODUCTTYPE_ISPASS = "@IsPass";

        private const string ARM_PRODUCTTYPE_STARTTIME = "@startTime";
        private const string ARM_PRODUCTTYPE_STOPTIME = "@stopTime";


        private const string ARM_PRODUCTTYPE_MO = "@MO";
        #endregion

        #region 数据检索部分
        public IList<AirtightnessInfo> LoadEntities()
        {
            return null;
        }

        /// <summary>
        /// 根据产品序列码 检索测试数据
        /// </summary>
        /// <typeparam name="T1">泛型</typeparam>
        /// <param name="productSN">产品序列码</param>
        /// <returns>AirtightnessInfo</returns>
        public AirtightnessInfo LoadEntity<T1>(T1 productSN)
        {
            SqlParameter parm = GetParameter(); 
            parm.Value = productSN;
            return ExcuteSqlToGetEntity("sp_selAirtightnessbyLastSN", parm);
        }

        /// <summary>
        /// 根据产品序列码 检索测试数据
        /// </summary>
        /// <param name="productSN"></param>
        /// <returns></returns>
        public IList<AirtightnessInfo> Airtightness_SelectbySN(string productSN)
        {
            SqlParameter parm = GetParameter(); 
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("sp_selAirtightnessbySN", parm);
        }


        /// <summary>
        /// 根据测试时间 检索测试数据
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="stopTime">结束时间</param>
        /// <returns></returns>
        public IList<AirtightnessInfo> Airtightness_SelectbyDT(DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetDTParameters(); 
            parms[0].Value = startTime;
            parms[1].Value = stopTime;
            return ExcuteSqlToGetEntities("sp_selAirtightnessbyDT", parms);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productTypeID"></param>
        /// <param name="startTime"></param>
        /// <param name="stopTime"></param>
        /// <returns></returns>

        public IList<AirtightnessInfo> Airtightness_SelectbyPIDDT(string productTypeID, DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetPIDDTParameters(); 
            parms[0].Value = productTypeID;
            parms[1].Value = startTime;
            parms[2].Value = stopTime;
            return ExcuteSqlToGetEntities("sp_selAirtightnessbyPIDDT", parms);
        }





        /// <summary>
        /// 
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="stopTime"></param>
        /// <returns></returns>
        //public IList<ProductType_QInfo> Airtightness_GetQbyPIDDT(DateTime startTime, DateTime stopTime)
        //{
        //    SqlParameter[] parms = GetDTParameters(); 
        //    parms[0].Value = startTime;
        //    parms[1].Value = stopTime;

        //    IList<ProductType_QInfo> Entities = new List<ProductType_QInfo>();
        //    using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_QbyPIDDT", parms))
        //    {
        //        while (rdr.Read())
        //        {
        //            ProductType_QInfo Entity = new ProductType_QInfo(rdr.GetValue(0).ToString(), rdr.GetDecimal(1), rdr.GetInt32(2), rdr.GetInt32(3));
        //            Entities.Add(Entity);
        //        }
        //    }
        //    return Entities;
        //}

        //public IList<Department_PRInfo> Airtightness_GetOPRbyIPDT(DateTime startTime, DateTime stopTime)
        //{
        //    SqlParameter[] parms = GetDTParameters();
        //    //Bind the parameter
        //    parms[0].Value = startTime;
        //    parms[1].Value = stopTime;

        //    IList<Department_PRInfo> Entities = new List<Department_PRInfo>();
        //    using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_OPRbyIPDT", parms))
        //    {
        //        while (rdr.Read())
        //        {
        //            Department_PRInfo Entity = new Department_PRInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetDecimal(2), rdr.GetInt32(3), rdr.GetInt32(4));
        //            Entities.Add(Entity);
        //        }
        //    }
        //    return Entities;
        //}

        //public IList<Department_PRInfo> Airtightness_GetPRbyIPDT(DateTime startTime, DateTime stopTime)
        //{
        //    SqlParameter[] parms = GetDTParameters();
        //    //Bind the parameter
        //    parms[0].Value = startTime;
        //    parms[1].Value = stopTime;

        //    IList<Department_PRInfo> Entities = new List<Department_PRInfo>();
        //    using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_PRbyIPDT", parms))
        //    {
        //        while (rdr.Read())
        //        {
        //            Department_PRInfo Entity = new Department_PRInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetDecimal(2), rdr.GetInt32(3), rdr.GetInt32(4));
        //            Entities.Add(Entity);
        //        }
        //    }
        //    return Entities;
        //}
        #endregion


        #region 参数设置部分
        public SqlParameter[] GetDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(ARM_PRODUCTTYPE_STARTTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(ARM_PRODUCTTYPE_STOPTIME, SqlDbType.DateTime, 8),
                };
            return parms;
        }

        public SqlParameter[] GetPIDDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(ARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50),
                    new SqlParameter(ARM_PRODUCTTYPE_STARTTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(ARM_PRODUCTTYPE_STOPTIME, SqlDbType.DateTime, 8),
                };
            return parms;
        }

        public SqlParameter[] GetPIDParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(ARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50) 
                };
            return parms;
        }

        public SqlParameter[] GetMOParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(ARM_PRODUCTTYPE_MO, SqlDbType.NVarChar, 50),
                  
                };
            return parms;
        }

        #endregion


        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override AirtightnessInfo GetEntityRecord(SqlDataReader rdr)
        {
            AirtightnessInfo aro = new AirtightnessInfo()
            {
                ProductSN = rdr.GetValue(0).ToString(),
                RecordTime = rdr.GetDateTime(1),
                ProductTypeID = rdr.GetValue(2).ToString(),
                UserID = rdr.GetValue(3).ToString(),
                PCIP = rdr.GetValue(4).ToString(),
                TestValue = rdr.GetInt32(5),
                IsPass = rdr.GetBoolean(6)
            };
            return aro;
        }

        public override AirtightnessInfo GetEntityRecord()
        {
            AirtightnessInfo aro = new AirtightnessInfo();
            return aro;
        }

        public override string GetInsertsp()
        {
            return "sp_InsertAirtightness";
        }

        public override string GetUpdatesp()
        {
            return null;
        }

        public override string GetDeletesp()
        {
            return null;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(ARM_PRODUCTTYPE_PRODUCTSN, SqlDbType.NVarChar, 50);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(ARM_PRODUCTTYPE_PRODUCTSN, SqlDbType.NVarChar, 50),
                    new SqlParameter(ARM_PRODUCTTYPE_RECORDTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(ARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50),
                    new SqlParameter(ARM_PRODUCTTYPE_USERID, SqlDbType.NVarChar, 10),
                    new SqlParameter(ARM_PRODUCTTYPE_PCIP,SqlDbType.NVarChar,50),
					new SqlParameter(ARM_PRODUCTTYPE_TESTVALUE, SqlDbType.Int, 4),
                    new SqlParameter(ARM_PRODUCTTYPE_ISPASS, SqlDbType.Bit, 1)

                };
            return parms;
        }

        public override void SetParameters(AirtightnessInfo productType, params SqlParameter[] Parms)
        {
            Parms[0].Value = productType.ProductSN;
            Parms[1].Value = productType.RecordTime;
            Parms[2].Value = productType.ProductTypeID;
            Parms[3].Value = productType.UserID;
            Parms[4].Value = productType.PCIP;
            Parms[5].Value = productType.TestValue;
            Parms[6].Value = productType.IsPass;
        }
        #endregion 
    
        #region IAirtightness 成员


     

        #endregion

        #region IAirtightness 成员


        public IList<AirtightnessInfo> GetAirtightnessByMO(string moID)
        {
            SqlParameter[] parm = GetMOParameters();
            parm[0].Value = moID;
            return ExcuteSqlToGetEntities("sp_selAirtightnessbyMO", parm);
        }


        public IList<AirtightnessInfo> Airtightness_SelectbyPType(string ptype)
        {
            SqlParameter[] parms = GetPIDParameters();
            parms[0].Value = ptype;
            return ExcuteSqlToGetEntities("sp_selAirtightnessbyPID", parms);
        }


        public AirtightnessInfo AirSelectbySN(string productSN)
        {
            SqlParameter parm = GetParameter(ARM_PRODUCTTYPE_PRODUCTSN);
            parm.Value = productSN;
            return ExcuteSqlToGetEntity("sp_selAirtightnessbySN", parm);
        }

        private static SqlParameter GetParameter(string parmname)
        {
            SqlParameter parm = new SqlParameter(parmname, SqlDbType.NVarChar, 50);
            return parm;
        }
 
        #endregion
    }
}
